<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Job extends CIC_A_Controller
{
	var $category_tree;
	function __construct()
	{
		parent::__construct();
		$this->init();
	}
	
	function init()
	{
		$this->load->helper('ckeditor');
		$this->category_tree 	= $this->get_category_tree(4);
	}
	
	function index()
	{		
		$this->admin_priv(__METHOD__);
		$data					= $this->get_job();
		$data['category_tree']	= $this->category_tree;
		$this->load->view('admin/job/index',$data);
	}
	
	function job_add()
	{
		$data = array(
				'category_tree' 	=> $this->category_tree,
				'ckeditor'			=> $this->ckeditor_config('ckeditor'),
				'time_now'			=> date('Y-m-d',strtotime('20 day')),
		);
		$this->load->view('admin/job/job_add',$data);
	}
	
	function job_edit()
	{
		$data = array(
				'category_tree' 	=> $this->category_tree,
				'ckeditor'			=> $this->ckeditor_config('ckeditor'),
				'job'			=> $this->cic_model->get_one('job',array('job_id'=>$this->uri->segment(4))),
		);
		$this->load->view('admin/job/job_edit',$data);
	}
	
	function get_job()
	{
		$where	= '';
		$and 	= '';
		$url = site_url('admin/job/index?');
		$category_id = $this->input->get('category_id');
		$key_words	 = $this->input->get('key_words');
		if(! empty($category_id))
		{
			$where .= 'category_id = '.$category_id;
			$and	= ' and ';
			$url   .= 'category_id='.$category_id;
		}
		if(! empty($key_words))
		{
			$where .= $and."job_title like '%$key_words%'";
			$url   .= '&key_words='.$key_words;
		}
		$data['page']	 = $this->page($url, $this->cic_model->get_total_rows('job',$where), $this->per_page,TRUE);
		$data['job'] 	 = $this->cic_model->get_all('job',$where,array('job_create_time','desc'),$this->per_page,$this->input->get('per_page'));
		return $data;
	}
	
	function delete_job()
	{
		$this->admin_priv(__METHOD__);
		$result = $this->cic_model->delete('job',array('job_id'=>$this->input->post('job_id')));
		if($result)
			echo 1;
		else
			echo 0;
	}
	
	function add_job()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('job', 'admin/job/content_add');
		$data = array(
				'category_id'		=> $this->input->post('category_id'),
				'job_title'			=> $this->input->post('job_title',FALSE),
				'job_content'		=> $this->input->post('job_content',FALSE),
				'job_attribute'		=> serialize($this->input->post('job_attribute')),
				'job_source'		=> $this->input->post('job_source'),
				'job_author'		=> $this->input->post('job_author'),
				'job_thumb_image'	=> $this->input->post('job_thumb_image'),
				'job_zhiding'		=> $this->input->post('job_zhiding'),
				'job_tuijian'		=> $this->input->post('job_tuijian'),
				'job_keywords'		=> $this->input->post('job_keywords'),
				'job_desc'			=> $this->input->post('job_desc'),
				'job_hits'			=> $this->get_config('ciccms/admin','sys','hits')?$this->get_config('ciccms/admin','sys','hits'):mt_rand(1,500),
				'job_new_window'	=> $this->input->post('job_new_window'),
				'job_create_time'	=> time(),
				'job_user_add'		=> $this->session->userdata('admin_user'),
		);
		if($this->cic_model->insert('job',$data))
		{
			$this->message->msg('添加成功！',site_url('admin/job/index'));
		}else{
			$this->message->msg('添加失败！',site_url('admin/job/job_add'),5);
		}
	}
	
	function edit_job()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('job', 'admin/job/job_edit/'.$this->uri->segment(4));
		$data = array(
				'category_id'		=> $this->input->post('category_id'),
				'job_title'			=> $this->input->post('job_title',FALSE),
				'job_content'		=> $this->input->post('job_content',FALSE),
				'job_attribute'		=> serialize($this->input->post('job_attribute')),
				'job_source'		=> $this->input->post('job_source'),
				'job_author'		=> $this->input->post('job_author'),
				'job_thumb_image'	=> $this->input->post('job_thumb_image'),
				'job_zhiding'		=> $this->input->post('job_zhiding'),
				'job_tuijian'		=> $this->input->post('job_tuijian'),
				'job_keywords'		=> $this->input->post('job_keywords'),
				'job_desc'			=> $this->input->post('job_desc'),
				'job_hits'			=> $this->get_config('ciccms/admin','sys','hits')?$this->get_config('ciccms/admin','sys','hits'):mt_rand(1,500),
				'job_new_window'	=> $this->input->post('job_new_window'),
				'job_create_time'	=> time(),
				'job_user_add'		=> $this->session->userdata('admin_user'),
		);
		if($this->cic_model->update('job',$data,array('job_id'=>$this->input->post('job_id'))))
		{
			$this->message->msg('修改成功！',site_url('admin/job/index'));
		}else{
			$this->message->msg('未做任何修改！',site_url('admin/job/job_edit/'.$this->uri->segment(4)));
		}
	}
}